const Http = {
	request(url, method, data = {}, header = {}) {
		header = {
			// 'content-type': 'application/json',
			'content-type': 'application/x-www-form-urlencoded',
			...header,
		}
		data = {
			token: wx.getStorageSync('token'),
			platform: 'wechat',
			...data
		}
		return new Promise((resolve, reject) => {
			wx.request({
				url, method, data, header,
				success: res => {
					if (res.statusCode === 200) {
						resolve(res.data)
					} else {
						reject({
							status: res.statusCode
						})
					}
				},
				fail: err => {
					reject({
						status: -1,
						err: err.errMsg
					})
				}
			})
		})
	},

	get(url, data, header) {
		return new Promise((resolve, reject) => {
			this.request(url, 'GET', data, header).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},

	post(url, data, header) {
		return new Promise((resolve, reject) => {
			this.request(url, 'POST', data, header).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	}
}

module.exports = Http